package com.wz.controller;

import com.domain.Result;
import com.domain.User;
import com.github.pagehelper.PageInfo;
import com.wz.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    //@Autowired(required=false)
    @Reference
    private UserService userService;

    //注册 在postman里面写如下请求 :  http://localhost:8081/user/register?userName=123&password=123
    @PostMapping("/register")
    public Result register(User user) {
        boolean flag = userService.register(user);
        return new Result(flag);
        //postman成功返回:  {
        //    "flag": true,
        //    "msg": null,
        //    "data": null
        //}
    }

    //查询所有用户,实际上就是分页查询而已 在postman上查询: http://localhost:8081/user/pageQuery
    @GetMapping("/pageQuery")//@RequestParam参数可以设置初始值
    public Result pageQuery(@RequestParam(required = false, defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "100") Integer size) {
        PageInfo pageInfo = userService.pageQuery(page, size);
        return new Result(true, pageInfo);
    }

    //修改用户信息
    @PutMapping("/updateByUuid")
    public Result updateByUuid(User user){
        boolean flag = userService.update(user);
        return new Result(flag);
        /*postman里面的路径这么写:http://localhost:8081/user/updateByUuid?uuid=1&userName=张三三&password=123&realName=zss&gender=0&birthday=1992/02/02
        * 返回ok
        * 上述是根据uuid来修改用户信息,而且用户信息修改成功!!       
        * */
    }
    
    //根据uuid查询单个用户
    @GetMapping("/{uuid}")
    @ResponseBody
    public Result findByUuid(@PathVariable("uuid") Integer uuid){
        User oneUser = userService.findByUuid(uuid);
        System.out.println(oneUser);
        return new Result(!(oneUser==null),oneUser);
        /*postman里面的地址请求这么写:http://localhost:8081/user/2
        * 可以查询到用户信息
        * @PathVariable注解的意义是:可以吧uuid封装到参数请求中
        * */
    }
    
    
    //根据uuid删除用户信息
    @DeleteMapping("/{uuid}")
    public Result deleteByUuid (@PathVariable("uuid") Integer uuid){
        int i = userService.deleteByUuid(uuid);
        return new Result(i==1);
        /*postMan的请求url: http://localhost:8081/user/3
        * 此时数据库已经删除成功uuid=3的数据
        * 
        * */
    }
 
   
   

}
